import re
import time
import requests
from logger import LOG


def get_proxy():
    """
    调用代理ip接口,获取代理ip值,并且将这些代理ip值和京东码进行关联存储
    :return: 返回代理ip的值
    """
    while True:
        try:
            # 代理ip url
            url = 'http://gec.ip3366.net/api/?key=***************&getnum=1&anonymoustype=3&area=1&formats=2'
            resp = requests.get(url=url, timeout=5)
            data_json = resp.text
            ip = re.findall(r'"Ip":"(.*?)",', data_json)[0]  # 获取到的ip
            port = re.findall(r'"Port":(.*?),', data_json)[0]  # 获取到的端口号
            proxy = {'http': ip + ':' + port, }
        except:
            time.sleep(10)
            LOG.info('调用代理ip错误')
            continue
        break
    LOG.info('%s' % (proxy))
    return proxy


if __name__ == '__main__':
    get_proxy()
